layui.define(['jquery', 'jqelem','layer','form'], function(exports) {
    var $ = layui.jquery,
        app = layui.data('app'),
        zzpValidation= function(){};
    var danger = "layui-form-danger";
    var Validator = {
            textbox: {
                required: '不能为空',
                min: 'Hetta økið skal í minsta lagi vera {characters} stavir',
                max: 'Hetta økið skal í mesta lagi vera {characters} stavir',
                email: '邮箱错误',
                url: 'Vev adressan er ikki gildig',
                number: '不为数字',
                digits: '不为数字'
            }

    }
    zzpValidation.prototype.validation = function (form) {
        var hasErrors = false;
        var _this = this;
        $(form).find('input, select, textarea').each(function () {
            var regex = null;
            // noinspection JSAnnotator
            // noinspection JSAnnotator
            if ($(this).is('input') && ($(this).attr('type') == 'text' || $(this).attr('type') == undefined)) {
                if ($(this).attr('data-required') != undefined && $.trim($(this).val()) == '' && $(this).attr('data-required-if') == undefined) {
                    _this.showMsg(this,Validator.textbox.required)
                    hasErrors = true;
                    return false;
                }
                if ($(this).attr('data-type') != undefined) {
                    switch ($(this).attr('data-type')) {
                        case 'email':
                            regex = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
                            if (!regex.test($(this).val()) && $(this).val() != '') {
                                _this.showMsg(this, Validator.textbox.email);
                                hasErrors = true;
                            }
                            break;
                        case 'number':
                            regex = /^\s*(\+|-)?((\d+([\.,]\d+)?)|([\.,]\d+))\s*$/;
                            if (!regex.test($(this).val()) && $(this).val() != '') {
                                _this.showMsg(this, Validator.textbox.number);
                                hasErrors = true;
                            }
                            break;
                        case 'digits':
                            regex = /^\s*\d+\s*$/;
                            if (!regex.test($(this).val()) && $(this).val() != '') {
                                _this.showMsg(this, Validator.textbox.digits);
                                hasErrors = true;
                            }
                            break;
                    }
                    if (hasErrors){
                        return !hasErrors;
                    }
                }
            }
            // Select
            if ($(this).is('select')) {
                // required
                if ($(this).attr('data-required') != undefined && $(this).attr('data-required-if') == undefined && ($(this).val()==''||$(this).val() == null)) {
                    /*layui select*/
                    _this.showMsg(this,Validator.textbox.required);
                    hasErrors = true;
                    return false;
                }
                // required-if & required-if-value
                if ($(this).attr('data-required-if') != undefined && $(this).val() == '' && (($(this).attr('data-required-if-value') == undefined && $('#' + $(this).attr('data-required-if')).is(':checked')) || ($(this).attr('data-required-if-value') != undefined && $('#' + $(this).attr('data-required-if')).val() == $(this).attr('data-required-if-value')))) {
                    _this.showMsg(this,Validator.textbox.required);
                    hasErrors = true;
                    return false;
                }

            }
            // // Input[type=checkbox]
            // if ($(this).is('input') && $(this).attr('type') == 'checkbox') {
            //     // required
            //     if ($(this).attr('data-required') != undefined && !$(this).is(':checked') && $(this).attr('data-required-if') == undefined) {
            //         _this.showMsg(this,Validator.textbox.required)
            //         hasErrors = true;
            //         return false;
            //     }
            //     // required-if & required-if-value
            //     if ($(this).attr('data-required-if') != undefined && $(this).val() == '' && (($(this).attr('data-required-if-value') == undefined && $('#' + $(this).attr('data-required-if')).is(':checked')) || ($(this).attr('data-required-if-value') != undefined && $('#' + $(this).attr('data-required-if')).val() == $(this).attr('data-required-if-value')))) {
            //         _this.showMsg(this,Validator.textbox.required)
            //         hasErrors = true;
            //         return false;
            //     }
            // }
            // // Textarea
            // if ($(this).is('textarea')) {
            //     // required
            //     if ($(this).attr('data-required') != undefined && $(this).val() == '' && $(this).attr('data-required-if') == undefined) {
            //         _this.showMsg(this,Validator.textbox.required);
            //         hasErrors = true;
            //         return false;
            //     }
            //     // required-if & required-if-value
            //     if ($(this).attr('data-required-if') != undefined && $(this).val() == '' && (($(this).attr('data-required-if-value') == undefined && $('#' + $(this).attr('data-required-if')).is(':checked')) || ($(this).attr('data-required-if-value') != undefined && $('#' + $(this).attr('data-required-if')).val() == $(this).attr('data-required-if-value')))) {
            //         _this.showMsg(this,Validator.textbox.required);
            //         hasErrors = true;
            //         return false;
            //     }
            //     // patterns
            //     if ($(this).attr('data-type') != undefined) {
            //         switch ($(this).attr('data-type')) {
            //             case 'url':
            //                 regex = /^(https?:\/\/)?((([a-z\d]([a-z\d-]*[a-z\d])*)\.)+[a-z]{2,}|((\d{1,3}\.){3}\d{1,3}))(\:\d+)?(\/[-a-z\d%_.~+]*)*(\?[;&a-z\d%_{},.~+=-]*)?(\#[-a-z\d_]*)?$/i;
            //                 if (!regex.test($(this).val()) && $(this).val() != '') {
            //                     _this.showMsg(this,Validator.textbox.required);
            //                     hasErrors = true;
            //                 }
            //                 break;
            //         }
            //         if (hasErrors){
            //             return !hasErrors;
            //         }
            //     }
            // }
        });
        return !hasErrors;
    };
    zzpValidation.prototype.showMsg = function(obj,msg){
        var _msg=$(obj).parent().prev("label").html();
        if(_msg!='' &&_msg !='undefined'){
            _msg=_msg+msg;
        }else{
            _msg = msg;
        }
        layer.msg(_msg, {icon: 5,shift: 6});
        $(obj).focus().addClass(danger);
    }

    var zzpValidation= new zzpValidation();
    /**
     *暴露接口
     */
    exports("zzpValidation", zzpValidation);
});